<template>
  <view class="login-page">
    <view class="logo-area"></view>
    <view class="title">数字人面试官</view>
    <view class="subtitle">智能、高效、公平的面试体验</view>
    <view class="form-area">
      <input class="input" v-model="phone" type="number" maxlength="11" placeholder="请输入手机号" />
      <input class="input" v-model="password" type="password" placeholder="请输入临时密码" />
      <button class="login-btn" @click="handleLogin" :loading="loading">登录</button>
    </view>
    <view class="tips">
      <view>登录提示：</view>
      <view>1. 使用管理员提供的手机号和临时密码登录</view>
      <view>2. 请在预约的面试时间段内登录系统</view>
      <view>3. 登录后请立即完成身份验证</view>
    </view>
  </view>
</template>

<script>
import request from '../../utils/request'

export default {
  data() {
    return {
      phone: '',
      password: '',
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      if (!this.phone || !this.password) {
        uni.showToast({ title: '请输入手机号和临时密码', icon: 'none' });
        return;
      }

      this.loading = true;
      try {
        const res = await request.post('/api/candidate/login', {
          phone: this.phone,
          password: this.password
        });

        if (res.code === 200) {
          // 保存token和用户信息
          uni.setStorageSync('token', res.data.token);
          uni.setStorageSync('candidateInfo', res.data.candidate);
          
          uni.showToast({ title: '登录成功', icon: 'success' });
          
          // 跳转到面试首页
          setTimeout(() => {
            uni.reLaunch({ url: '/pages/interviewHome/interviewHome' });
          }, 1000);
        } else {
          // 显示具体的错误信息
          const errorMsg = res.message || '登录失败';
          uni.showModal({
            title: '登录失败',
            content: errorMsg,
            showCancel: false
          });
        }
      } catch (error) {
        console.error('登录请求失败:', error);
        // 显示具体的错误信息
        const errorMsg = error.data?.message || error.message || '网络错误，请稍后重试';
        uni.showModal({
          title: '登录失败',
          content: errorMsg,
          showCancel: false
        });
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #f6f6f6;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 80rpx;
}
.logo-area {
  width: 120rpx;
  height: 120rpx;
  background: #07C160;
  border-radius: 24rpx;
  margin-bottom: 32rpx;
}
.title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}
.subtitle {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 48rpx;
}
.form-area {
  width: 80vw;
  max-width: 600rpx;
  display: flex;
  flex-direction: column;
  gap: 32rpx;
  margin-bottom: 40rpx;
}
.input {
  height: 48px;
  border: none;
  border-bottom: 1px solid #e5e5e5;
  background: #fff;
  font-size: 16px;
  padding: 0 12px;
  margin-bottom: 8px;
  border-radius: 0;
  outline: none;
}
.input:focus {
  border-bottom: 1.5px solid #07C160;
}
.login-btn {
  margin-top: 16rpx;
  height: 44px;
  background: #07C160;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  border: none;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(7,193,96,0.08);
}
.tips {
  margin-top: 40rpx;
  color: #999;
  font-size: 14px;
  text-align: left;
  opacity: 0.9;
  width: 80vw;
  max-width: 600rpx;
}
</style> 